package com.web.base.join;

import com.web.base.core.facade.AreaFacade;
import com.web.core.join.AreaJoin;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* 区域跨库连接信息处理器
* 
* @author zhengjin
* @since 1.0 2018年06月27日
*/
public class AreaJoinHandler
{
    private AreaJoinHandler()
    {
    }

    /**
     * 跨库连接区域信息
     *
     * @param list
     * @param areaFacade
     */
    public static void areaJoin(List<? extends AreaJoin> list, AreaFacade areaFacade)
    {
        if (list == null || list.isEmpty() || areaFacade == null)
            return;

        Set<Integer> idList = new HashSet<>();
        for (AreaJoin join : list)
        {
            if (join.getProvinceId() != null && join.getProvinceId() > 0)
                idList.add(join.getProvinceId());

            if (join.getCityId() != null && join.getCityId() > 0)
                idList.add(join.getCityId());

            if (join.getAreaId() != null && join.getAreaId() > 0)
                idList.add(join.getAreaId());
        }
        Map<Integer, String> map = areaFacade.getNameList(idList);
        for (AreaJoin join : list)
        {
            if (join.getProvinceId() != null && join.getProvinceId() > 0)
                join.setProvinceName(map.get(join.getProvinceId()));

            if (join.getCityId() != null && join.getCityId() > 0)
                join.setCityName(map.get(join.getCityId()));

            if (join.getAreaId() != null && join.getAreaId() > 0)
                join.setAreaName(map.get(join.getAreaId()));
        }
    }

    public static void areaJoin(AreaJoin join, AreaFacade areaFacade)
    {
        if (join == null || areaFacade == null)
            return;

        Set<Integer> idList = new HashSet<>();
        if (join.getProvinceId() != null && join.getProvinceId() > 0)
            idList.add(join.getProvinceId());

        if (join.getCityId() != null && join.getCityId() > 0)
            idList.add(join.getCityId());

        if (join.getAreaId() != null && join.getAreaId() > 0)
            idList.add(join.getAreaId());

        Map<Integer, String> map = areaFacade.getNameList(idList);
        if (join.getProvinceId() != null && join.getProvinceId() > 0)
            join.setProvinceName(map.get(join.getProvinceId()));

        if (join.getCityId() != null && join.getCityId() > 0)
            join.setCityName(map.get(join.getCityId()));

        if (join.getAreaId() != null && join.getAreaId() > 0)
            join.setAreaName(map.get(join.getAreaId()));
    }

}
